package com.frxuexi.park.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil.ItemCallback
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ItemParkBinding

class ParkAdapter : PagingDataAdapter<ParkBeanItem, ParkAdapter.ParkViewHolder>(diff) {

    private var mOnItemClickListener: OnItemClickListener?=null
    private var mOnItemLongClickListener: OnItemLongClickListener?=null

    companion object {
        val diff = object : ItemCallback<ParkBeanItem>() {
            override fun areItemsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(oldItem: ParkBeanItem, newItem: ParkBeanItem): Boolean {
                return oldItem == newItem
            }
        }
    }

    class ParkViewHolder(itemView: View) : ViewHolder(itemView) {
        val binding = DataBindingUtil.bind<ItemParkBinding>(itemView)
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val item = getItem(position)!!
        holder.binding?.bean = item
        holder.binding?.parkStatus?.apply {
            when (item.spaceStatus) {
                "空闲" -> {
                    setTextColor(holder.itemView.context.getColor(com.frxuexi.lib_base.R.color.green))
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_status)
                }

                "已占用" -> {
                    setTextColor(holder.itemView.context.getColor(com.frxuexi.lib_base.R.color.blue))
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_zhanyong)
                }

                else -> {
                    setTextColor(holder.itemView.context.getColor(com.frxuexi.lib_base.R.color.darkorange))
                    setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_weihu)
                }
            }
        }


        holder.itemView.apply {
            setOnClickListener {
                mOnItemClickListener?.onItemClick(item.id!!)
            }

            setOnLongClickListener {
                mOnItemLongClickListener?.onItemLongClick()
                return@setOnLongClickListener true
            }

        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_park, null)
        return ParkViewHolder(inflate)
    }

    fun setOnItemClickListener(listener: OnItemClickListener){
        mOnItemClickListener=listener
    }
    fun setOnItemLongClickListener(listener: OnItemLongClickListener){
        mOnItemLongClickListener=listener
    }
}

interface OnItemClickListener{
    fun onItemClick(id: Int)
}

interface OnItemLongClickListener{
    fun onItemLongClick():Boolean
}